package com.alibaba.aliper.server.model.domain;

import java.util.Collection;
import java.util.Vector;

import com.alibaba.aliper.server.model.interfaces.LineControll;
import com.alibaba.aliper.server.model.interfaces.LineView;

public class Line implements LineView , LineControll {
	private String id;

	public Line(String id) {
		this.id = id;
	}

	private Vector<Point> points = new Vector<Point>();
	private Object lock = new Object();
	private double min = 0;
	public double getMin() {
		return min;
	}

	public double getMax() {
		return max;
	}

	private double max = 100;
	private boolean rangeAvailable = false;

	public Collection<Point> getPoints() {
		return this.points;
	}

	public void addPoint(long time, double data) {
		synchronized (this.lock) {
			this.points.add(new Point(time, data));
			this.lock.notifyAll();
		}
	}

	public String getId() {
		return id;
	}

	@Override
	public Collection<Point> getPointsAfter(int index)
			throws InterruptedException {
		synchronized (this.lock) {
			if (this.points.size() < index + 1) {
				return null;
				//this.lock.wait();
			}
			return new Vector<Point>(this.points.subList(index, this.points.size()));
		}
		
	}

	public Point getPoint(int index) throws InterruptedException {
		synchronized (this.lock) {
			while(true){
				if (this.points.size() < index + 1) {
					this.lock.wait();
				} else {
					break;
				}
			}
		}
		return this.points.get(index);
	}
	
	public boolean isRangeAvailable() {
		return rangeAvailable;
	}
	

	public void setRange(double min, double max) {
		this.rangeAvailable  = true;
		this.min = min;
		this.max = max;
	}

	@Override
	public double getRangeMax() {
		return this.min;
	}

	@Override
	public double getRangeMin() {
		return this.max;
	}

	@Override
	public Point getFirstPoint() {
		if(this.points.size() > 0)
			return this.points.get(0);
		return null;
	}

}
